﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;

class FileSplitter
{
    private const int PART_SIZE = 8*10;

    static void Main()
    {
        string filePath = @"..\..\FileSplitter.cs";
        string password = "tainamaina";
        string restoreDirPath = Directory.GetCurrentDirectory();        
        FileInfo file = new FileInfo(filePath);

        SplitFile(filePath,file.Name);
        CombineFiles(restoreDirPath, file);
    }
  
    private static void CombineFiles(string restoreDirPath, FileInfo file)
    {
        DirectoryInfo restoreDir = new DirectoryInfo(restoreDirPath);
        FileInfo[] files = restoreDir.GetFiles(file.Name + ".*");
        using (FileStream resultWriter = new FileStream(file.Name + "-original" + file.Extension, FileMode.Create))
        {
            foreach (var part in files)
            {
                using (FileStream partReader = new FileStream(part.FullName, FileMode.Open))
                {
                    byte[] buffer = new byte[PART_SIZE];
                    int bytePartRead = partReader.Read(buffer, 0, PART_SIZE);
                    resultWriter.Write(buffer, 0, bytePartRead);
                }
            }
        }
    }
  
    private static void SplitFile(string filePath,string fileName)
    { 
        using (FileStream reader = new FileStream(filePath, FileMode.Open, FileAccess.Read))
        {
            int partCounter = 0;
            while (reader.Position != reader.Length)
            {
                partCounter++;
                byte[] buffer = new byte[PART_SIZE];
                int bytesRead = reader.Read(buffer, 0, PART_SIZE);
                //EncryptBytes(buffer,
                using (FileStream writer = new FileStream(fileName +"."+ string.Format("{0:0000}",partCounter), FileMode.Create))
                {
                    writer.Write(buffer, 0, bytesRead);                                    
                }
            }
        }
    }
}